﻿@page "/admin/settings/advanced"
@using System.Reflection
@Html.AntiForgeryToken()
@{
    var settings = BlogConfig.AdvancedSettings;

    var asm = Assembly.GetEntryAssembly();
    var currentVersion = (new Version(asm!.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version)).ToString();
}
@section scripts {
    <script type="module">
        import * as settings from '/js/app/admin.settings.module.js';

        function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            setInterval(function () {
                minutes = parseInt(timer / 60, 10);
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? '0' + minutes : minutes;
                seconds = seconds < 10 ? '0' + seconds : seconds;

                display.text(minutes + ':' + seconds);

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
        }

        $('.btn-reset').click(function () {
            callApi(`/api/settings/reset`, 'POST', {}, () => { });
            $('.btn-reset').text('Wait...');
            $('.btn-reset').addClass('disabled');
            $('.btn-reset').attr('disabled', 'disabled');
            startTimer(30, $('.btn-reset'));
            setTimeout(function () {
                location.href = '/';
            }, 30000);
        });

        $('.btn-check-update').click(function () {
            $('.spinner-border').show();
            $('.alert-has-new-release').hide();
            $('.alert-no-new-release').hide();
            $('.btn-get-update').addClass('disabled');
            $('.btn-get-update').attr('href', '#');

            var updateCheckCanvas = new bootstrap.Offcanvas(document.getElementById('updateCheckCanvas'));
            updateCheckCanvas.show();

            fetch('https://api.github.com/repos/EdiWang/Moonglade/releases/latest', {
                method: 'GET',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
            }).then(async (resp) => {
                var data = await resp.json();
                $('.spinner-border').hide();

                $('#currentAssemblyFileVersion').val('@currentVersion');
                $('#releaseName').val(data.name);
                $('#releaseTagName').val(data.tag_name);
                $('#releaseCreatedAt').val(new Date(data.created_at));

                var c = settings.compareVersionNumbers(data.tag_name.replace('v', ''), '@currentVersion');
                var hasNewVersion = c == 1 && !data.prerelease;

                if (hasNewVersion) {
                    $('.alert-has-new-release').show();
                    $('.btn-get-update').removeClass('disabled');
                    $('.btn-get-update').attr('href', data.html_url);
                }
                else {
                    $('.alert-no-new-release').show();
                }
            }).catch(err => {
                blogToast.error(err);
                console.error(err);
            });
        });

        const form = document.querySelector('#form-settings');
        form.addEventListener('submit', settings.handleSettingsSubmit);
    </script>
}

@section admintoolbar {
    <partial name="_SettingsHeader" />
}

<form id="form-settings" asp-controller="Settings" asp-action="Advanced">
    <div class="admin-settings-entry-container">
        <div class="row">
            <div class="col-md-6">
                <div class="mb-4">
                    <h4 class="admin-subtitle fw-bold mb-2">@SharedLocalizer["Advanced Settings"]</h4>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-arrow-right-circle settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnablePingback" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="EnablePingback" value="false">
                                <input type="checkbox" name="EnablePingback" value="true" class="form-check-input" @(settings.EnablePingback ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-map settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableSiteMap" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="EnableSiteMap" value="false">
                                <input type="checkbox" name="EnableSiteMap" value="true" class="form-check-input" @(settings.EnableSiteMap ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-search settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableOpenSearch" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="EnableOpenSearch" value="false">
                                <input type="checkbox" name="EnableOpenSearch" value="true" class="form-check-input" @(settings.EnableOpenSearch ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-diagram-2 settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableFoaf" class="form-check-label"></label>
                            <div class="form-text">@SharedLocalizer["* Requires restarting application"]</div>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="EnableFoaf" value="false">
                                <input type="checkbox" name="EnableFoaf" value="true" class="form-check-input" @(settings.EnableFoaf ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-filetype-xml settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableOpml" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="EnableOpml" value="false">
                                <input type="checkbox" name="EnableOpml" value="true" class="form-check-input" @(settings.EnableOpml ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white">
                        <div class="col-auto">
                            <i class="bi-exclamation-triangle settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.WarnExternalLink" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch form-control-lg">
                                <input type="hidden" name="WarnExternalLink" value="false">
                                <input type="checkbox" name="WarnExternalLink" value="true" class="form-check-input" @(settings.WarnExternalLink ? "checked" : null)>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="mb-3">
                    <h4 class="admin-subtitle fw-bold mb-2">@Html.DisplayNameFor(m => settings.RobotsTxtContent)</h4>
                    <div class="p-3 rounded-3 shadow-sm border bg-white mb-3">
                        <textarea asp-for="@settings.RobotsTxtContent" class="form-control textarea-code" cols="60" rows="10" spellcheck="false"></textarea>
                    </div>

                    <h4 class="admin-subtitle fw-bold mb-2">@Html.DisplayNameFor(m => settings.FootScripts)</h4>
                    <div class="p-3 rounded-3 shadow-sm border bg-white mb-3">
                        <div class="alert alert-warning">
                            Using a third party script may cause your website to be blocked by some browsers or ad blockers, it can have security risks too. Please use with caution.
                        </div>

                        <textarea asp-for="@settings.FootScripts" class="form-control textarea-code" cols="60" rows="10" spellcheck="false"></textarea>

                        <div class="form-text">
                            * Global script that will be injected into every page's footer, before the ending of <code>body</code> tag. Typically used for third party analytics services. <br/>

                            * Please use valid <code>&lt;script&gt;...&lt;/script&gt;</code> code.
                        </div>
                    </div>
                </div>
                <div>
                    <h4 class="admin-subtitle fw-bold mb-2">
                        @SharedLocalizer["Options"]
                    </h4>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white mb-2">
                        <div class="col-auto">
                            <i class="bi-moon-stars settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["Update"]
                            <div class="form-text">
                                @SharedLocalizer["Check online for new Moonglade release."]
                            </div>
                        </div>
                        <div class="col-md-5 text-end">
                            <a href="#" class="btn btn-sm btn-outline-accent btn-check-update">
                                @SharedLocalizer["Check now"]
                            </a>
                        </div>
                    </div>

                    <div class="settings-entry2 row align-items-center py-3 px-2 rounded-3 shadow-sm border bg-white">
                        <div class="col-auto">
                            <i class="bi-eraser settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["System Reset"]
                            <div class="form-text">
                                @SharedLocalizer["Erase all data and configuration."]
                            </div>
                        </div>
                        <div class="col-md-3 text-end">
                            <a href="javascript:;" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#resetModal">
                                @SharedLocalizer["Reset"]
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="admin-settings-action-container border-top pt-2 mt-2">
        <input id="btn-save-settings" type="submit" value="@SharedLocalizer["Save"]" class="btn btn-outline-accent" />
    </div>
</form>

<partial name="_RestartModal" />
<partial name="_ResetModal" />
<partial name="_UpdateCheckCanvas" />